class AccountController < ApplicationController
    # Be sure to include AuthenticationSystem in Application Controller instead
    include AuthenticatedSystem
    # If you want "remember me" functionality, add this before_filter to Application Controller
    before_filter :login_from_cookie
    
    # say something nice, you goof!  something sweet.
    def index
        if logged_in? || User.count > 0
            redirect_to :controller => 'index'
        else
            redirect_to :action => 'signup'
        end
    end

    def login
        return unless request.post?
        self.current_user = User.authenticate(params[:login], params[:password])
        if logged_in?
            if params[:remember_me] == "1"
                self.current_user.remember_me
                cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
            end
            flash[:notice] = "登录成功!"            
            redirect_to :controller => 'index'
        else
            flash[:error] = "用户名或密码错误!"
        end
    end

    def signup
        flash[:error] = nil
        @user = User.new(params[:user])
        return unless request.post?
        @user.save!
        self.current_user = @user
        flash[:notice] = "注册成功!"        
        redirect_to :controller => 'index'
    rescue ActiveRecord::RecordInvalid
        render :action => 'signup'
    end
  
    def logout
        self.current_user.forget_me if logged_in?
        cookies.delete :auth_token
        reset_session
        flash[:notice] = "用户已注销."
        redirect_to :action => 'login'
    end
end
